package com.llt.user.controller;

import com.llt.enum_util.ResponseBodyEnum;
import com.llt.pojo.common.AjaxResponseBody;
import com.llt.utils.EmailUtil;
import com.llt.utils.RedisUtil;
import com.llt.utils.Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@CrossOrigin
@Api(value = "工具",tags = {"工具"})
public class UtilsController {

    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/eMailCode")
    @ApiOperation(value = "获取验证码")
    public AjaxResponseBody geteMailCode(@ApiParam (value = "email",name = "邮箱",required = true) String email){
        if (email==null && email==""){
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_OTHER);
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(email);
        if (m.matches()){
          String code =  Utils.codeGet();
            System.out.println("邮箱"+email+"验证码"+code);
            try {
                Utils.sendMailFactory().sendSimpleMail(email,code);
                redisUtil.set(email,code,(60*2));
            }catch (MailException e){
                e.printStackTrace();
            }
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK);
        }
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_OTHER);
    }
}
